package GUI.Components;

import B3D_Elements.Others.B3D_MotionEvent;
import other.Wizard;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A JButton, used to set the Color of an Object
 *
 * @author David
 */
public class ColorButton extends JButton
{

    Color color;

    /**
     * Creates a ColorButton, used to set the Color of an Object
     *
     * @param initColor the Color the Button shall be initialized with
     */
    public ColorButton(Color initColor)
    {
        setPreferredSize(new Dimension(30, 20));
        if (initColor == null)
        {
            setText("Select Color");
        }
        color = initColor;
        addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                color = JColorChooser.showDialog(ColorButton.this, "Select Color", ColorButton.this.getBackground());
            }
        });
    }

    /**
     *
     * @param initColor
     * @param b3D_Spatial_MotionPath
     */
    public ColorButton(Color initColor, final B3D_MotionEvent b3D_Spatial_MotionPath)
    {
        setPreferredSize(new Dimension(30, 20));
        if (initColor == null)
        {
            setText("Select Color");
        }
        color = initColor;
        addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                color = JColorChooser.showDialog(ColorButton.this, "Select Color", ColorButton.this.getBackground());
                b3D_Spatial_MotionPath.getMotionPath().setColor(Wizard.makeColorRGBA(color));
            }
        });
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (color != null)
        {
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    /**
     * Returns the current Color
     *
     * @return
     */
    public Color getColor()
    {
        return color;
    }

    /**
     *
     * @param col
     */
    public void setColor(Color col)
    {
        color = col;
    }
}
